﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClientWPF.localhost;
using System.Text.RegularExpressions;

namespace ClientWPF
{
    public partial class EditProductForm : Window
    {
        Service1 service = new Service1();
        AuthentificationCredentials authentification = new AuthentificationCredentials();

        public EditProductForm()
        {
            InitializeComponent();
            WindowStartupLocation = System.Windows.WindowStartupLocation.CenterScreen;
        }

        private void EditProductButton_Click(object sender, RoutedEventArgs e)
        {
            authentification.Privileges = "admin";
            service.AuthentificationCredentialsValue = authentification;

            Product product = new Product();
            product.Name = NameBox.Text;
            Match match = Regex.Match(PriceBox.Text, "[0-9]*\\,[0-9]*");

            if (!QuantityBox.Text.Equals("")) product.Quantity = Convert.ToInt32(QuantityBox.Text);
            
            if (!(NameBox.Text.Equals("")))
            {
                if (match.Success)
                {
                    product.Price = Convert.ToDouble(PriceBox.Text);
                    service.EditProduct(product);
                    MessageBox.Show("Product " + product.Name + " edited successfully!");
                    Close();
                    }
                else MessageBox.Show("Price must be in format: [0-9]*,[0-9]*");
            }
            else MessageBox.Show("One or more fields are blanc!");
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            Close();
        }

    }
}
